﻿@model ExportPreviewModel

@{
    ViewBag.Title = T("Common.Export") + " - " + T("Admin.Common.Preview");

    var gridViewData = new ViewDataDictionary(ViewData);
    gridViewData["Parent"] = Model;

    string gridId = null;
    switch (Model.EntityType)
    {
        case ExportEntityType.Product:
        case ExportEntityType.Order:
        case ExportEntityType.Category:
        case ExportEntityType.Manufacturer:
        case ExportEntityType.Customer:
        case ExportEntityType.NewsletterSubscription:
        case ExportEntityType.ShoppingCartItem:
            gridId = Model.EntityType.ToString();
            break;
    }
}

<div class="section-header">
    <div class="title">
        <a sm-backto asp-action="Edit" asp-route-id="@Model.Id" title="@T("Common.Back")"></a>
        <img src="@Url.Content(Model.ThumbnailUrl)" />
        <span>@ViewBag.Title - @Model.Name</span>
    </div>
    <div class="options">
        @if (Model.LogFileExists)
        {
            <a asp-action="DownloadLogFile" asp-route-id="@Model.Id" class="btn btn-secondary" target="LogFile">
                <i class="far fa-file-lines"></i>
                <span>@T("Admin.Configuration.ActivityLog")</span>
            </a>
        }
    </div>
</div>

<form id="ProfileExportForm" asp-action="Execute" method="post">
    <input type="hidden" asp-for="Id" />
    <input type="hidden" name="SelectedIds" value="" />

    <div class="preview-grid-container">
        @if (gridId.HasValue())
        {
            <partial name="_Grid.Preview.@(gridId)" model="null" view-data="gridViewData" />
        }
        else
        {
            <div class="alert alert-warning">
                @T("Admin.DataExchange.Export.NoPreview")
            </div>
        }
    </div>
</form>

<script sm-target-zone="scripts" data-origin="ExportPreview">
	$(function() {
		// Execute export.
		$('#ExportAll, #ExportSelected').on('click', function (e) {
			e.preventDefault();

            var gridId = '@gridId.EmptyNull().ToLower()';
			var form = $('#ProfileExportForm');
			var exportAll = $(this).attr('id') === 'ExportAll';
            var selectedIds = '';

            if (!exportAll) {
                const grid = $('#' + gridId + '-preview-grid').parent().data('datagrid');
                selectedIds = grid.selectedRowKeys.join(',');
            }
            
			form.find('[name=SelectedIds]').val(selectedIds);
			form.submit();
			return false;
		});
	});
</script>